package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC811 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<String> subdomainVisits(String[] cpdomains) {
            Map<String, Integer> cntMap = new HashMap<>();
            for (String cpdomain : cpdomains) {
                int cnt = Integer.parseInt(cpdomain.split(" ")[0]);
                String url = cpdomain.split(" ")[1];
                StringBuilder curBuilder = new StringBuilder();
                final String[] split = url.split("\\.");
                for (int i = split.length - 1; i >= 0; i--) {
                    curBuilder.insert(0, split[i] + (i == split.length - 1 ? "" : "."));
                    cntMap.put(curBuilder.toString(), cntMap.getOrDefault(curBuilder.toString(), 0) + cnt);
                }
            }

            List<String> res = new ArrayList<>();
            for (String sub : cntMap.keySet()) {
                res.add(cntMap.get(sub) + " " + sub);
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        String[] arr = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
        System.out.println(solution.subdomainVisits(arr));
    }

    public static void main(String[] args) throws Exception {
        LC811 an = new LC811();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
